﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Navigation;

namespace JumpstartApp.Helpers
{
    public class LiveSdk
    {
        public class LiveId
        {
            // used by Live ID

            public LiveId(IDictionary<string, object> result) : this(result, null) { }
            public LiveId(IDictionary<string, object> result, Uri image)
            {
                Id = result.ContainsKey("id") ? result["id"] as string : string.Empty;
                NameFull = result.ContainsKey("name") ? result["name"] as string : string.Empty;
                NameFirst = result.ContainsKey("first_name") ? result["first_name"] as string : string.Empty;
                NameLast = result.ContainsKey("last_name") ? result["last_name"] as string : string.Empty;
                Gender = result.ContainsKey("gender") ? result["gender"] as string : string.Empty;
                Locale = result.ContainsKey("locale") ? result["locale"] as string : string.Empty;

                Uri _Uri;
                if (Uri.TryCreate(result.ContainsKey("link") ? result["link"] as string : string.Empty, UriKind.Absolute, out _Uri))
                    Link = _Uri;

                DateTime _Date;
                if (DateTime.TryParse(result.ContainsKey("updated_time") ? result["updated_time"] as string : string.Empty, out _Date))
                    UpdatedTime = _Date;

                var _Emails = result.ContainsKey("emails") ? result["emails"] as IDictionary<string, object> : new Dictionary<string, object>();
                EmailPreferred = _Emails.ContainsKey("preferred") ? _Emails["preferred"] as string : string.Empty;
                EmailAccount = _Emails.ContainsKey("account") ? _Emails["account"] as string : string.Empty;

                if (image != null)
                {
                    PhotoLocation = image;
                    var _PhotoBitmap = new Windows.UI.Xaml.Media.Imaging.BitmapImage(image);
                    PhotoBitmap = _PhotoBitmap;
                }
            }

            public string Id { get; set; }
            public string NameFull { get; set; }
            public string NameFirst { get; set; }
            public string NameLast { get; set; }
            public Uri Link { get; set; }
            public string Gender { get; set; }
            public string Locale { get; set; }
            public DateTime UpdatedTime { get; set; }
            public string EmailAccount { get; set; }
            public string EmailPreferred { get; set; }
            public Uri PhotoLocation { get; set; }
            public Windows.UI.Xaml.Media.Imaging.BitmapImage PhotoBitmap { get; set; }
        }

        public static async Task<bool?> LoginAsync(bool logout)
        {
            try
            {
                var _Client = new Microsoft.Live.LiveAuthClient() { Theme = Microsoft.Live.ThemeType.Light };

                if (logout && _Client.CanLogout)
                    _Client.Logout();

                LoginResult = await _Client.LoginAsync(new string[] { "wl.signin", "wl.basic", "wl.emails", "wl.skydrive", "wl.skydrive_update" });
                if (LoginResult.Status != Microsoft.Live.LiveConnectSessionStatus.Connected)
                    return false;

                Client = new Microsoft.Live.LiveConnectClient(LoginResult.Session);
                var _MeResult = (await Client.GetAsync("me")).Result;

                dynamic _PhotoResult = (await Client.GetAsync("me/picture")).Result;
                var _PhotoLocation = new Uri(_PhotoResult.location, UriKind.Absolute);

                User = new LiveId(_MeResult, _PhotoLocation);
                return true;
            }
            catch (Exception)
            {
                System.Diagnostics.Debugger.Break();
                return null;
            }
        }

        public static Microsoft.Live.LiveLoginResult LoginResult { get; private set; }
        public static Microsoft.Live.LiveConnectClient Client { get; private set; }
        public static LiveId User { get; private set; }
    }
}
